package com.macro.mall.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.TiTrainingSignRecordLog;
import com.macro.mall.service.TiTrainingSignService;
import com.macro.mall.utils.FilePortUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * TiTrainingSign的路由接口服务
 *
 * @author
 */
@Slf4j
@RestController
@RequestMapping("TiTrainingSignController")
@Api(tags = "签到签退信息表", description = "签到签退信息表")
public class TiTrainingSignController {

    /**
     * TiTrainingSignService服务
     */
    @Autowired
    private TiTrainingSignService tiTrainingSignService;

    /**
     * 查询所有数据的方法
     * 统计
     * @param
     * @return
     */
    @GetMapping(value = "/list", produces = {"application/json;charset=UTF-8"})
    public CommonResult find(@RequestParam(value = "courseName",required = false) String courseName,
                             @RequestParam(value = "communityName",required = false) String communityName,
                             @RequestParam(value = "phone",required = false) String phone,
                             @RequestParam(value = "userIdentity",required = false) String userIdentity,
                             @RequestParam(value = "signStatus",required = false) String signStatus,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        return CommonResult.success(CommonPage.restPage(tiTrainingSignService.find(courseName,communityName,phone,userIdentity,signStatus,pageNum,pageSize)));
    }

    /**
     * 通过id查询数据的方法
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/getById", produces = {"application/json;charset=UTF-8"})
    public CommonResult findOne(@RequestParam(name = "id") Integer id) {
        return CommonResult.success(tiTrainingSignService.findOne(id));
    }


    /**
     * 插入属性不为空的数据方法
     *
     * @param
     * @return
     */
    @PostMapping(value = "/save", produces = {"application/json;charset=UTF-8"})
    public CommonResult save(@Valid @RequestBody TiTrainingSignRecordLog value) {
        tiTrainingSignService.saveNotNull(value);
        return CommonResult.success(null);
    }

    /**
     * 更新属性不为空的数据方法
     *
     * @param
     * @return
     */
    @PostMapping(value = "/update", produces = {"application/json;charset=UTF-8"})
    public CommonResult update(@Valid @RequestBody TiTrainingSignRecordLog value) {
        CommonResult data = tiTrainingSignService.updateNotNullById(value);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "签到结果导出")
    @RequestMapping(value = "/exportResultList", method = RequestMethod.GET)
    @ResponseBody
    public void  exportSignResultList(  @RequestParam(value = "courseName",required = false) String courseName,
                                        @RequestParam(value = "communityName",required = false) String communityName,
                                        @RequestParam(value = "phone",required = false) String phone,
                                        @RequestParam(value = "userIdentity",required = false) String userIdentity,
                                        @RequestParam(value = "signStatus",required = false) String signStatus,
                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "10000") Integer pageSize,
//            @RequestBody List<TiTrainingSignRecordLog> request,
                                      HttpServletResponse response) {
        try {
            //导出的表名
            String title = "签到结果";
            //表中第一行表头字段
            String[] headers = {"学院课程", "社区", "身份信息", "姓名", "手机号","签到时间","签退时间","完成观看时间"};
            List<TiTrainingSignRecordLog> result = tiTrainingSignService.find(courseName,communityName,phone,userIdentity,signStatus,pageNum,pageSize);
            //具体需要写入excel需要哪些字段，这些字段取自UserReward类，也就是上面的实际数据结果集的泛型
            List<String> listColumn = Arrays.asList("courseName", "communityName", "userIdentityName", "name", "phone", "signInTime", "signOutTime", "updateTime");
            try {
                FilePortUtil.exportExcel(response, title, headers, result, listColumn);
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }catch (Exception e) {
            log.error(e.getMessage());
        }
    }
    /**
     * 通过id删除数据方法
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/deleteId", produces = {"application/json;charset=UTF-8"})
    public CommonResult delete(@RequestParam(name = "id") Integer id) {
        CommonResult data = tiTrainingSignService.deleteById(id);
        return CommonResult.success(null);
    }
}
